package feeinterface.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.ulwx.tool.DbUtils;
import com.ulwx.tool.ObjectUtils;
import feeinterface.modeldb.BaseImsiPhone;
/**
 * IMSI与手机号码的对应表
 * 
 * @author 黄杰
 * 
 */
public class BaseImsiPhoneDao {

	private static final Logger log = Logger.getLogger(BaseImsiPhoneDao.class);

	/**
	 * 根据手机IMSI号码获取对象
	 *  
	 * @param imsi
	 * @return
	 * @throws Exception
	 */
	public static BaseImsiPhone getByIMSI(String imsi) throws Exception {
		BaseImsiPhone bi = new BaseImsiPhone();
		bi.setIMSI(imsi);

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, imsi);
		List<BaseImsiPhone> list = DbUtils.doQueryClassNoSql(null,
				BaseImsiPhone.class, bi, "IMSI", map);
		if (list.size() >= 1)
			return list.get(0);
		return null;
	}
	
	public static BaseImsiPhone getPhoneBy(String imsi) throws Exception {
		String sql="select * from BaseImsiPhone where imsi=? order by UpdateTime desc limit 1 ";

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, imsi);
		List<BaseImsiPhone> list = DbUtils.doQueryClass(null, BaseImsiPhone.class,
				sql, map);
		if (list.size() >= 1)
			return list.get(0);
		return null;
	}

	/**
	 * 保存IMSI与手机号码的对应记录
	 * 
	 * @param bi
	 * @throws Exception
	 */
	public static void Insert(BaseImsiPhone bi) {
		try {
			String sql = "INSERT INTO BaseImsiPhone (CityID,Phone,ProvinceID,IMSI,UpdateTime)" +
					     " VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE CityID=?,ProvinceID=?,UpdateTime=?";
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(1, bi.getCityID());
			map.put(2, bi.getPhone());
			map.put(3, bi.getProvinceID());
			map.put(4, bi.getIMSI());
			map.put(5, bi.getUpdateTime());
			map.put(6, bi.getCityID());
			map.put(7, bi.getProvinceID());
			map.put(8, bi.getUpdateTime());
			DbUtils.executeBindUpdate(null, sql, map);
		} catch (Exception e) {
			log.error("", e);

		}
	}

	/**
	 * 批量保存
	 * 
	 * @param ppsts
	 * @throws Exception
	 */
	public void insert(BaseImsiPhone[] ppsts) throws Exception {
		String sql = "INSERT INTO BaseImsiPhone (CityID,Phone,ProvinceID,IMSI,UpdateTime) VALUES (?,?,?,?,?)";
//		DataBase db = null;
		try {
//			db = DataBaseFactory.getDataBase();
			List<Map<Integer, Object>> args = new ArrayList<Map<Integer, Object>>();

			for (int i = 0; i < ppsts.length; i++) {
				BaseImsiPhone pfst = ppsts[i];
				Map<Integer, Object> vars = new HashMap<Integer, Object>();
				vars.put(1, pfst.getCityID());
				vars.put(2, pfst.getPhone());
				vars.put(3, pfst.getProvinceID());
				vars.put(4, pfst.getIMSI());
				vars.put(5, pfst.getUpdateTime());
				args.add(vars);
			}

			DbUtils.executeBindBatch(null,sql, args);
		} catch (Exception ex) {
//			db.rollback();
			throw ex;
		} 
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		System.out.println(ObjectUtils.toJsonString(getPhoneBy("123")));

	}

}
